package club.wx3609.images.controller;

import club.wx3609.images.Utils.IsNullUtils;
import club.wx3609.images.entity.Message;
import club.wx3609.images.interfaces.Constant;
import club.wx3609.images.service.MessageService;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
public class MessageController {

    //定义消息返回的数量
    private Integer messageNumber=20;
    @Resource
    private MessageService messageService;

    @PostMapping("/getMessages")
    public String findMessage(String sender,String receiver){
        if(IsNullUtils.character(sender)&&IsNullUtils.character(receiver)){
            List<Message> Messages=messageService.findAll();
            List<String> resultMessage=new ArrayList<String>();
            List<String> msg=new ArrayList<String>();
            for(Message messageAll:Messages){
                if(
                        (messageAll.getSender().equals(sender.trim())&&
                                messageAll.getReceiver().equals(receiver.trim()))||
                                (messageAll.getSender().equals(receiver.trim())&&
                                        messageAll.getReceiver().equals(sender))
                ){
                    if(messageAll.getSender().equals(sender.trim())){
                        resultMessage.add("<div class='dataMessage'>\n" +
                                "        <div>\n" +
                                "            <span class='userNumber'>"+messageAll.getSender()+"</span>&nbsp;<span class='nowTime fm'>"+messageAll.getMessageDate()+"</span>\n" +
                                "        </div>\n" +
                                "        <div>\n" +
                                "            "+ messageAll.getMessageContent()+"\n" +
                                "        </div>\n" +
                                "    </div>");
                    }else if(messageAll.getSender().equals(receiver.trim())){
                        resultMessage.add("<div class='dataMessage right'>\n" +
                                "        <div>\n" +
                                "            <span class='userNumber'>"+messageAll.getSender()+"</span>&nbsp;<span class='nowTime fm'>"+messageAll.getMessageDate()+"</span>\n" +
                                "        </div>\n" +
                                "        <div>\n" +
                                "            "+ messageAll.getMessageContent()+"\n" +
                                "        </div>\n" +
                                "    </div>");
                    }
                }
            }

            if(resultMessage.size()>messageNumber){
                for(int i=0;i<messageNumber;i++){
                    msg.add(resultMessage.get(i));
                }
                Collections.reverse(msg);
            }else{
                msg=resultMessage;
            }

            return JSON.toJSONString(msg);
        }
        return Constant.inputJudge0;
    }

}
